package com.sakiko.stream.controller;

import com.sakiko.stream.service.ChatAssistant;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * projectName: com.sakiko.stream.controller
 *
 * @author: sakiko
 * time: 2025/8/27 23:21
 * description:
 */
@RestController
@Slf4j
public class StreamingChatModelController {

    @Resource // low-level LLM API
    private StreamingChatModel streamingChatLanguageModel;

    @Resource // high-level LLM API
    private ChatAssistant chatAssistant;

    // http://localhost:9007/chatstream/chat?prompt=讲个笑话
    // 前台响应
    @GetMapping("/chatstream/chat")
    public Flux<String> chat(@RequestParam("prompt") String prompt) {
        System.out.println("StreamingChatModelController.chat...come to chat");

        return Flux.create(emitter -> {
            streamingChatLanguageModel.chat(prompt, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String partialResponse) {
                    emitter.next(partialResponse); // 下一步
                }

                @Override
                public void onCompleteResponse(ChatResponse completeResponse) {
                    emitter.complete(); // 结束信号: 正常结束
                }

                @Override
                public void onError(Throwable error) {
                    emitter.error(error); // 结束信号: 异常结束
                }
            });
        });
    }

    // http://localhost:9007/chatstream/chat2
    // 后台打印
    @GetMapping("/chatstream/chat2")
    public void chat2(@RequestParam(value = "prompt", defaultValue = "杭州有什么好吃的？") String prompt) {
        System.out.println("StreamingChatModelController.chat...come to chat2");

        streamingChatLanguageModel.chat(prompt, new StreamingChatResponseHandler() {
            @Override
            public void onPartialResponse(String partialResponse) {
                System.out.println("partialResponse = " + partialResponse);
            }

            @Override
            public void onCompleteResponse(ChatResponse completeResponse) {
                System.out.println("completeResponse = " + completeResponse);
            }

            @Override
            public void onError(Throwable error) {
                error.printStackTrace();
            }
        });
    }

    // http://localhost:9007/chatstream/chat3
    @GetMapping("chatstream/chat3")
    public Flux<String> chat3(@RequestParam(value = "prompt", defaultValue = "云原生和微服务有什么联系？") String prompt) {
        System.out.println("StreamingChatModelController.chat...come to chat3");

        return chatAssistant.chatFlux(prompt);
    }

}
